// pages/buying/index.js
var util = require('../../utils/util.js');
var $api = require('../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buyingList: [], //求购列表数组
    contributionId: "", //求购列表咨询id
    autosize: {
      maxHeight: 50,
      minHeight: 10
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (wx.getStorageSync("city") == "") {
      //获取用户地理信息
      let userInfo = wx.getStorageSync('userInfo')
      this.setData({
        city: userInfo.city
      })
    } else {
      this.setData({
        city: wx.getStorageSync("city")
      })
    }
    //获取求购列表
    this.getBuyingMessage();

  },

  xiajia: function (e) {
    var self = this;
    wx.showModal({
      title: '提示',
      content: '确认下架该求购吗？',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          $api.buyingMessage({id: e.currentTarget.dataset.id, status: 3}).then(res => {
            if (res.code == 1) {
              self.getBuyingMessage();
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  
  delItem: function (e) {
    var self = this;
    wx.showModal({
      title: '提示',
      content: '确认删除该求购吗？',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          $api.requestDel({id: e.currentTarget.dataset.id}).then(res => {
            if (res.code == 1) {
              self.getBuyingMessage();
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // delete(e) {
  //   console.log(e);
  // },

  //获取求购列表
  getBuyingMessage: function (e) {
    let params;
    $api.getMyBuyingMessageList(params).then(res => {
      if (res.code == 1) {
        this.setData({
          buyingList: res.data
        })
        console.log(this.data.buyingList)
      }
    })
  },
  //打开回复面板
  openReplyPopup: function (e) {
    this.setData({
      contributionId: e.target.dataset.id,
      showReplyPopup: true
    })
  },
  onCloseReplyPopup: function (e) {
    this.setData({
      contributionId: "",
      content: "",
      showReplyPopup: false
    })
  },
  //咨询
  reply: function (e) {
    if (this.data.content == '') {
      wx.showToast({
        title: '回复内容不能为空',
        icon: 'none',
        duration: 2000
      })
    } else {
      let params = {
        contribution_id: this.data.contributionId,
        content: this.data.content,
      }
      $api.comment(params).then(res => {
        if (res.code == 1) {
          wx.showToast({
            title: '发送成功!',
            icon: 'none',
            duration: 2000
          })
          this.getBuyingMessage();
          //清空数据
          this.setData({
            contributionId: "",
            content: "",
            showReplyPopup: false
          })
        }
      })
    }
  },


  //预览图片
  previewImg: function (e) {
    console.log(e.currentTarget.dataset.src);
    console.log(e.currentTarget);
    wx.previewImage({
      current: e.currentTarget.dataset.src, //当前图片地址
      urls: e.currentTarget.dataset.imgs, //所有要预览的图片的地址集合 数组形式
    })
  },

  //删除求购
  delete: function (e) {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})